//
//  ModifyNickVC.m
//  sensen_ios
//
//  Created by mc on 16/2/24.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ModifyNickVC.h"
#import "ModifyTextField.h"

@interface ModifyNickVC ()
{
      ModifyTextField *_field; // 输入框
      UIButton *_confirmBtn; //确认按钮
}


@end

@implementation ModifyNickVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //修改昵称
    self.title = @"修改昵称";
    
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1];
    
    // 导航栏左边返回按钮
    [self createLeftBarButtonItemWithImagename:nil];

    // 创建邮箱输入框
    [self _createInputTextfield];
    //  创建确定按钮
    [self _createConfirmButton];
    // Do any additional setup after loading the view.
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 导航栏样式
    [self isNaviTranslucent:NO statesLightContent:YES];
    // 把当前邮箱显示在输入框
    UserModel *user = [CommonCache getUserInfo];
//    if (![user.reg_from isEqualToString:@"2"] ) {
        if (user.nickname.length > 0) {
            _field.text = user.nickname;
            
        }else{
            NSMutableString *name = [NSMutableString stringWithString:user.username];
            [name replaceCharactersInRange:NSMakeRange(4, 5) withString:@"****"];
            _field.text = name;
        }
        
        
//    }else{
//        _field.text = user.nickname;
//    }

    
}


// 创建邮箱输入框
- (void)_createInputTextfield
{
    _field = [[ModifyTextField alloc] initWithFrame:CGRectMake(20, 20, SCREEN_WIDTH - 40, 40) placeholder:@"请输入您的昵称"];
    [self.view addSubview:_field];
}

// 创建确认修改按钮
- (void)_createConfirmButton{
    _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _confirmBtn.frame = CGRectMake(20, 100, SCREEN_WIDTH - 40, 35);
    [self.view addSubview:_confirmBtn];
    [_confirmBtn setTitle:@"确认修改" forState:UIControlStateNormal];
    [_confirmBtn addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
    [_confirmBtn setBackgroundColor:SS_NAVI_BLUE_COLOR];
    
    _confirmBtn.layer.borderColor = SS_NAVI_BLUE_COLOR.CGColor;
    _confirmBtn.layer.borderWidth = 1;
    _confirmBtn.layer.masksToBounds = YES;
    _confirmBtn.layer.cornerRadius = 3.0;
}


// 保存按钮执行事件
- (void)saveAction{
    if (_field.text.length == 0) {
        [SVProgressHUD showOnlyStatus:@"请先输入昵称" withDuration:1.5];
        return;
    }
    
    
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
    
    // 创建警告弹窗
    NSString *alertTitle = @"确定要修改昵称？";
    NSString *yesButtonTitle = @"确定";
    NSString *cancelButtonTitle = @"取消";
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    // 添加点击动作
    // 取消
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:nil];
    // 确定
    UIAlertAction *yesAction = [UIAlertAction actionWithTitle:yesButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action)
                                {
                                    
                                    [SVProgressHUD showWithStatus:@"正在修改中..."];
                                    
                                    UserModel *model = [CommonCache getUserInfo];
                                    NSDictionary *params = @{@"uid":model.uid,
                                                             @"nickname":_field.text};
                                    // 发送网络请求
                                    [BoyeHttpClient post:@"BY_User_update" apiVerson:100 params:params success:^(AFHTTPRequestOperation *operation, id result) {
                                       
                                        
                                        
                                        //重新获取昵称信息
                                        [BoyeHttpClient post:@"BY_BbsUserCenter_getUserInfo" apiVerson:100 params:@{@"uid":model.uid} success:^(AFHTTPRequestOperation *operation, id result) {
                                             [SVProgressHUD showOnlyStatus:@"修改成功!" withDuration:1];
                                            
                                            if (![result isKindOfClass:[NSNull class]]) { // 有数据
                                            NSLog(@"昵称 ＝ %@", result[@"nickname"]);
                                            // 保存到本地
                                            model.nickname = result[@"nickname"];
                                            [CommonCache setUserInfo:model];
                                           
                                            }
                                            
                                            // 返回上一界面
                                            [self.navigationController popViewControllerAnimated:YES];
                                            
                    
                                        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                            
                                            
                                             [SVProgressHUD showOnlyStatus:error.domain withDuration:1.5];
                    
                        
                                        }];
                                        
                                        
                                        
                                    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                        [SVProgressHUD showOnlyStatus:error.domain withDuration:1.5];
                                        
                                    }];
                                }];
    
    // Add the actions.
    [alertController addAction:cancelAction];
    [alertController addAction:yesAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    }else{
        
        
        
        
        [SVProgressHUD showWithStatus:@"正在修改中..."];
        
        UserModel *model = [CommonCache getUserInfo];
        NSDictionary *params = @{@"uid":model.uid,
                                 @"nickname":_field.text};
        // 发送网络请求
        [BoyeHttpClient post:@"BY_User_update" apiVerson:100 params:params success:^(AFHTTPRequestOperation *operation, id result) {
            [SVProgressHUD showOnlyStatus:@"修改成功!" withDuration:1];
            // 保存到本地
            model.nickname = _field.text;
            [CommonCache setUserInfo:model];
            // 返回上一界面
            [self.navigationController popViewControllerAnimated:YES];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [SVProgressHUD showOnlyStatus:error.domain withDuration:1.5];
            
        }];
        
        
    

    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
